package com.hua.nailcampus.acl.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hua.nailcampus.acl.model.School;
import com.hua.nailcampus.acl.model.UserSchool;
import com.hua.nailcampus.acl.model.vo.UserSchoolVo;
import com.hua.nailcampus.acl.service.SchoolService;
import com.hua.nailcampus.constant.RedisConst;
import com.hua.nailcampus.utils.JwtHelper;
import com.hua.nailcampus.utils.Result;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author:chenjianhua
 * @Date:2023/12/1 17:05
 * @Version 1.8
 */
@RestController
@Slf4j
@RequestMapping("/nailCampus/school")
public class SchoolController {

    @Autowired
    private SchoolService schoolService;
    @Resource
    private RedisTemplate redisTemplate;

    /**
     * 获取所有高校
     * @return
     */
    @GetMapping("/getSchoolList")
    public Result  getSchoolList(){
        List<School> schoolList = (List<School>) redisTemplate.opsForValue().get(RedisConst.SCHOOL_KEY);
        if(schoolList ==null){
            schoolList = schoolService.getSchoolList();
        }
        return  Result.ok(schoolList);
    }

    /**
     * 获取根据用户ID学校信息
     * @param request
     * @return
     */
    @GetMapping("/getSchool")
    public Result getSchool(HttpServletRequest request){
        String token = request.getHeader("token");
        Integer userId = JwtHelper.getUserId(token);
        UserSchoolVo userSchoolVo =  schoolService.getSchool(userId);
        return Result.ok(userSchoolVo);
    }

    /**
     * 用户认定学校申请
     * @param userSchool
     * @return
     */
    @PostMapping("/addUserSchool")
    public Result addUserSchool(@RequestBody UserSchool userSchool){
        log.info("用户认定学校申请：{}",userSchool);
        schoolService.addUserSchool(userSchool);
        return Result.ok(null);
    }

    /**
     * 是否申请过认定
     * @param request
     * @return
     */
    @GetMapping("/isApply")
    public Result isApply(HttpServletRequest request){
        String token = request.getHeader("token");
        Integer userId = JwtHelper.getUserId(token);
        UserSchool userSchool = schoolService.isApply(userId);
        return Result.ok(userSchool);
    }

    @GetMapping("/web/getUserSchoolList/{current}/{limit}")
    public Result getUserSchoolList(@PathVariable Integer current,
                                    @PathVariable Integer limit,
                                    UserSchool userSchool){
        IPage<UserSchoolVo> userSchoolVoPage =  schoolService.getUserSchoolList(current,limit,userSchool);
        return Result.ok(userSchoolVoPage);
    }

    @PostMapping("/web/passUserSchool")
    public Result passAllUserSchool(){
        schoolService.passAllUserSchool();
        return Result.ok(null);
    }
}
